home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7172 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: engr.orst.edu!usenet
  2. From: Sreekanth Nagarajan <nagarasr@research.cs.orst.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: A beginners valiant effort.
  5. Date: Sun, 18 Feb 1996 17:22:11 -0800
  6. Organization: College of Engineering, Oregon State University
  7. Message-ID: <3127D0C3.54B@research.cs.orst.edu>
  8. References: <4g81kr$mrs@soap.news.pipex.net>
  9. NNTP-Posting-Host: ada.cs.orst.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/730)
  14. CC: neales@dial.pipex.com
  15.  
  16. I am not sure what is exactly your problem. But your code seems to have
  17. couple of problems.
  18.  
  19.  
  20.   1. Since get_string() takes a 'boolean' it is wise to pass a boolean
  21. value (true or false) while calling it. Many c++ compilers won't even
  22. compile the given code (atleast mine didn't) since you are trying to
  23. pass int in place of boolean.
  24.  
  25.   2. You have defined the string_val_xxx arrays to be of fixed size. In
  26. the given example, you are passing a 16 character string as
  27. string_val_one and it is exactly of 16-char size. But to store this
  28. string properly (with a terminating '\0') you need atleast 17
  29. characters. 
  30.  
  31.   The program when executed on my machine produced the following output:
  32.  
  33. 1is not floweringis flowering
  34.  
  35.   This is understandable. Here is what might have happened:
  36.  
  37.   In set_string(), when string1 is copied to string_val_one, there is no
  38. space to put the terminating null. Thus it spills over to the first
  39. character of string_val_two. (Normally two variables defined next to
  40. each other are allocated space in that order, by many compilers) This
  41. null is overwritten when second string is copied. Again your
  42. string_val_two also has 1-char less space for your string. But then the
  43. strcpy goes ahead and puts the null beyond the last character of
  44. string_val_two. Depending on your environ, this may work or may lead to
  45. a crash!
  46.  
  47.   When getstring executes, it sees that string_index is 1 and properly
  48. executes the true part of IF. This part prints your string_val_one.
  49. Since string_val_two immediately follows string_val_one and there is no
  50. terminating null in string_val_one, the contents of both are printed. 
  51.  
  52.   Hope things are clear now :)
  53.  
  54.   The rule is: if you are statically allocating space, allocate space
  55. larger than the longest possible string that you expect. This way you
  56. can avoid surprises (and possible crashes!)
  57.  
  58.   Happy C++-ing..
  59.  
  60. Sreekanth
  61. nagarasr@cs.orst.edu
  62.  
  63.  
  64. > please e-mail responses to neales@dial.pipex.com - Ta.
  65. > #include <iostream.h>
  66. > #include <string.h>
  67. > enum boolean{false, true};
  68. > class switc_string
  69. >         {
  70. >         private:
  71. >                 char string_val_one[16];
  72. >                 char string_val_two[12];
  73. >         public:
  74. >                 void set_string(char string1[], char string2[])
  75. >                 {
  76. >                 strcpy(string_val_one, string1);
  77. >                 strcpy(string_val_two, string2);
  78. >                 }
  79. >                 void get_string(boolean string_index)
  80. >                 {
  81. >                 cout << string_index;
  82. >                 if ( string_index )
  83. >                         {cout << string_val_one;}
  84. >                 else
  85. >                         {cout << string_val_two;}
  86. >                 }
  87. >         };
  88. > void main()
  89. > {
  90. > switc_string s1;
  91. > s1.set_string("is not flowering","is flowering");
  92. > cout << endl;
  93. > s1.get_string(1);
  94. > }
  95.